package org.gruposp2p.dnie.server.rest;

import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Scope;
//import com.sun.jersey.multipart.FormDataParam;

/**
 * Basado en la información extraida en este artículo:
 * http://technopaper.blogspot.com/2009/03/multiple-file-upload-using-restful-web.html
 *
 * @author jj
 */
@Path("fileupload")
@Component
@Scope("request")
public class FileUploadResource {

    /*
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @POST
    @Produces("text/plain")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String upload(@FormDataParam("file") InputStream fileInputStream,
                    /*@FormParam("file") FormDataContentDisposition contDisp,*/
	/*                   @FormDataParam("title") String title,
                    @FormDataParam("description") String description) {

            System.out.println("fileInputStream: " + fileInputStream);
            //System.out.println("contDisp: " + contDisp);
            System.out.println("title: " + title);
            System.out.println("description: " + description);

            return "boo";
    }*/

    @POST
    @Produces(MediaType.TEXT_PLAIN)
    public String loadFile(@Context HttpServletRequest request) {
        String resultStatus="fail";
        String fileRepository="I:\\testRepo\\";
        if (ServletFileUpload.isMultipartContent(request)) {
            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List<FileItem> items=null;
            try {
                items = upload.parseRequest(request);
            } catch (FileUploadException e) {
                e.printStackTrace();
            }
            if(items!=null) {
                Iterator<FileItem> iter = items.iterator();
                while (iter.hasNext()) {
                    FileItem item = iter.next();
                    if(!item.isFormField() && item.getSize() > 0) {
                        String fileName = processFileName(item.getName());
                        try {
                            item.write(new File(fileRepository+fileName));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        resultStatus="ok";
                    }
                }
            }
        }
        return resultStatus;
    }

    private String processFileName(String fileNameInput) {
        String fileNameOutput = fileNameInput.substring(
                fileNameInput.lastIndexOf("\\")+1,fileNameInput.length());
        return fileNameOutput;
    }

}
